package org.e_gold.service.impl;

import org.apache.log4j.Logger;
import org.e_gold.dao.CountryDAO;
import org.e_gold.model.crawler.livescore.Country;
import org.e_gold.service.CountryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Thang Nguyen
 */
@Service
public class CountryServiceImpl implements CountryService {

	private static final Logger logger = Logger.getLogger(CountryServiceImpl.class);

	@Autowired
	private CountryDAO countryDAO;

	@Override
	public Country getCountryPersisted(Country c) {
		Country ret = null;
		try{
			Country country = countryDAO.getUniqueByPropertyName("name", c.getName());
			if(country != null){
				country.setUrl(c.getUrl());
				country = countryDAO.update(country);
			}else{
				country = new Country(c.getName(), c.getUrl());
				country = countryDAO.insert(country);
			}
			ret = country;
		}catch(Exception ex){
			logger.error(ex);
			ex.printStackTrace();
		}
		return ret;
	}
}
